package com.lanou.musicbox.fragment.seekmusic;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou.musicbox.R;
import com.lanou.musicbox.adapter.seekmusic.EnterTheSeekMusic_SingerAdapter;
import com.lanou.musicbox.base.BaseFragment;
import com.lanou.musicbox.bean.Search_Singer;
import com.lanou.musicbox.utils.VolleySingleton;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 赵宝龙 on 15/10/24.
 * 搜索_歌手页面
 */
public class EnterTheSeekMusic_SingerFragment extends BaseFragment implements AdapterView.OnItemClickListener {
    private ListView singerListView;
    private EnterTheSeekMusic_SingerAdapter adapter;
    private List<Search_Singer.DataEntity> data;
    private VolleySingleton volleySingleton;
    private String newUrl;

    public static EnterTheSeekMusic_SingerFragment passData(Context mContext, String str) {
        EnterTheSeekMusic_SingerFragment fragment = new EnterTheSeekMusic_SingerFragment();
        Bundle bundle = new Bundle();
        bundle.putString("url", str);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.entertheseekmusic_singer, null);
        singerListView = (ListView) view.findViewById(R.id.singerListView);
        singerListView.setCacheColorHint(1);
        singerListView.setOnItemClickListener(this);
        volleySingleton = VolleySingleton.getVolleySingleton(getActivity());
        Bundle bundle = getArguments();
        String url = bundle.getString("url");
        newUrl = "http://so.ard.iyyin.com/s/artist?uid=866058021842951&f=f3040" +
                "&app=ttpod&hid=1135212123029932&alf=alf203200&net=2&size=50" +
                "&v=v8.2.0.2015091720&utdid=VSeGyjss0fEDAOqIktP5W%2BO4&s=s200&page=1" +
                "&q=" + url + "&imsi=460078098497828&tid=0";
        Log.i("searching_url", newUrl + "0000");
        initData();
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    private void initData() {
        data = new ArrayList<>();
//        String url = "http://so.ard.iyyin.com/s/artist?uid=866058021842951&f=f3040&app=ttpod&hid=1135212123029932&alf=alf203200&net=2&size=50&v=v8.2.0.2015091720&utdid=VSeGyjss0fEDAOqIktP5W%2BO4&s=s200&page=1&q=%E4%B8%80%E6%AC%A1%E5%B0%B1%E5%A5%BD&imsi=460078098497828&tid=0";
        StringRequest request = new StringRequest(newUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                Search_Singer ss = gson.fromJson(response, Search_Singer.class);
                data = ss.getData();
                adapter = new EnterTheSeekMusic_SingerAdapter(data, getActivity());
                singerListView.setAdapter(adapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(request);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Search_Singer.DataEntity entity = (Search_Singer.DataEntity) parent.getItemAtPosition(position);
        int urlId = entity.get_id();
        Log.i("ZBL", "66666666666666666666666"+urlId);
        EnterThe_Singer_Fragment fragment = new EnterThe_Singer_Fragment();
        Bundle bundle = new Bundle();
        bundle.putInt("id", urlId);
        fragment.setArguments(bundle);
        getFragmentManager()
                .beginTransaction()
                .addToBackStack(null)
                .replace(R.id.fill_main, fragment)
                .commit();
    }
}
